/*
 * SimpleNode.java - ScriptME
 * 
 * Copyright (c) 2009 Cesar Henriques <cesar at alttab.com.ar>.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Lesser Public License v2.1
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 * 
 * Based on FESI Project
 * 
 * Contributors:
 * 	Jean-Marc Lugrin - initial API and implementation
 * 	Cesar Henriques <cesar at alttab.com.ar> - J2ME Porting and Extensions
 */
/*
 * 
 */

package org.scriptme.ecmascript.ast;

import org.scriptme.ecmascript.exceptions.ProgrammingError;
import org.scriptme.ecmascript.parser.EcmaScript;
import org.scriptme.ecmascript.parser.EcmaScriptConstants;

// TODO: Auto-generated Javadoc
/**
 * The Class SimpleNode.
 */
public abstract class SimpleNode implements Node, EcmaScriptConstants {

	/** The parent. */
	protected Node parent;

	/** The children. */
	protected Node[] children;

	/** The id. */
	protected int id;

	/** The parser. */
	protected EcmaScript parser;

	/** The line. */
	protected int line = 0; // JMCL

	/**
	 * Instantiates a new simple node.
	 * 
	 * @param i
	 *            the i
	 */
	public SimpleNode(int i) {
		id = i;
	}

	/**
	 * Instantiates a new simple node.
	 * 
	 * @param p
	 *            the p
	 * @param i
	 *            the i
	 */
	public SimpleNode(EcmaScript p, int i) {
		this(i);
		parser = p;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.scriptme.ecmascript.ast.Node#jjtOpen()
	 */
	public void jjtOpen() {
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.scriptme.ecmascript.ast.Node#jjtClose()
	 */
	public void jjtClose() {
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.scriptme.ecmascript.ast.Node#jjtSetParent(org.scriptme.ecmascript.ast.Node)
	 */
	public void jjtSetParent(Node n) {
		parent = n;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.scriptme.ecmascript.ast.Node#jjtGetParent()
	 */
	public Node jjtGetParent() {
		return parent;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.scriptme.ecmascript.ast.Node#jjtAddChild(org.scriptme.ecmascript.ast.Node,
	 *      int)
	 */
	public void jjtAddChild(Node n, int i) {
		if (children == null) {
			children = new Node[i + 1];
		} else if (i >= children.length) {
			Node c[] = new Node[i + 1];
			System.arraycopy(children, 0, c, 0, children.length);
			children = c;
		}
		children[i] = n;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.scriptme.ecmascript.ast.Node#jjtGetChild(int)
	 */
	public Node jjtGetChild(int i) {
		return children[i];
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.scriptme.ecmascript.ast.Node#jjtGetNumChildren()
	 */
	public int jjtGetNumChildren() {
		return (children == null) ? 0 : children.length;
	}

	/**
	 * Accept the visitor. *
	 * 
	 * @param visitor
	 *            the visitor
	 * @param data
	 *            the data
	 * 
	 * @return the object
	 */
	public Object jjtAccept(EcmaScriptVisitor visitor, Object data) {
		return visitor.visit(this, data);
	}

	/**
	 * Accept the visitor. *
	 * 
	 * @param visitor
	 *            the visitor
	 * @param data
	 *            the data
	 * 
	 * @return the object
	 */
	public Object childrenAccept(EcmaScriptVisitor visitor, Object data) {
		if (children != null) {
			for (int i = 0; i < children.length; ++i) {
				children[i].jjtAccept(visitor, data);
			}
		}
		return data;
	}

	/*
	 * You can override these two methods in subclasses of SimpleNode to
	 * customize the way the node appears when the tree is dumped. If your
	 * output uses more than one line you should override toString(String),
	 * otherwise overriding toString() is probably all you need to do.
	 */

	// JMCL
	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#toString()
	 */
	public String toString() {
		return EcmaScriptTreeConstants.jjtNodeName[id];
	}

	/**
	 * To string.
	 * 
	 * @param prefix
	 *            the prefix
	 * 
	 * @return the string
	 */
	public String toString(String prefix) {
		return prefix + toString();
	}

	/*
	 * Override this method if you want to customize how the node dumps out its
	 * children.
	 */

	/**
	 * Dump.
	 * 
	 * @param prefix
	 *            the prefix
	 */
	public void dump(String prefix) {
		System.out.println(toString(prefix));
		if (children != null) {
			for (int i = 0; i < children.length; ++i) {
				SimpleNode n = (SimpleNode) children[i];
				if (n != null) {
					n.dump(prefix + " ");
				}
			}
		}
	}

	// JMCL
	/**
	 * Sets the line number.
	 * 
	 * @param line
	 *            the new line number
	 */
	public void setLineNumber(int line) {
		this.line = line;
	}

	/**
	 * Gets the line number.
	 * 
	 * @return the line number
	 */
	public int getLineNumber() {
		return line;
	}

	/**
	 * Assert no children.
	 */
	public void assertNoChildren() {
		if (jjtGetNumChildren() > 0) {
			throw new ProgrammingError("AST Should have no children");
		}
	}

	/**
	 * Assert one child.
	 */
	public void assertOneChild() {
		if (jjtGetNumChildren() != 1) {
			throw new ProgrammingError("AST Should have 1 child");
		}
	}

	/**
	 * Assert two children.
	 */
	public void assertTwoChildren() {
		if (jjtGetNumChildren() != 2) {
			throw new ProgrammingError("AST Should have 2 children");
		}
	}

	/**
	 * Assert three children.
	 */
	public void assertThreeChildren() {
		if (jjtGetNumChildren() != 3) {
			throw new ProgrammingError("AST Should have 3 children");
		}
	}

	/**
	 * Assert four children.
	 */
	public void assertFourChildren() {
		if (jjtGetNumChildren() != 4) {
			throw new ProgrammingError("AST Should have 4 children");
		}
	}

}
